<template>
  <div>
    <Nav />
    <div class="page-box">

      <!-- 筛选区域 -->
      <div class="search-box">
        <!-- :inline="true" 表单在一行显示 -->
        <el-form size="mini" :inline="true" class="demo-form-inline">
          <el-form-item label="分类名称">
            <el-input v-model.trim="query.catename" placeholder="请输入分类名称" />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="search">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="reset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 工具条区域 -->
      <div class="tabbar-box">
        <el-row type="flex" justify="space-between">
          <div>
            <el-button plain type="primary" icon="el-icon-plus" size="mini" @click="$refs.myDialog.dialogVisible=true">新增</el-button>
            <el-button plain type="warning" icon="el-icon-download" size="mini" @click="exportData">导出</el-button>
          </div>
          <div>
            <el-button icon="el-icon-arrow-down" size="mini" circle />
            <el-button icon="el-icon-refresh" size="mini" circle />
            <el-button icon="el-icon-menu" size="mini" circle />
          </div>
        </el-row>
      </div>
      <!-- 表格区域 -->
      <div class="table-box">
        <el-table
          :data="list"
          style="width: 100%"
          border
        >
          <el-table-column type="index" label="#" width="50" />
          <el-table-column prop="id" label="分类ID" />
          <el-table-column prop="catename" label="分类名称" />
          <el-table-column label="图标class">
            <template #default="{row}">
              {{ row.icon }} <i class="fa " :class="row.icon" />
            </template>
          </el-table-column>
          <el-table-column prop="sort_num" label="权重" />
          <el-table-column align="center" label="操作">
            <template #default="{row}">
              <el-row type="flex" justify="center">
                <el-button circle type="primary" icon="el-icon-edit" size="mini" @click="$refs.myDialog.updateCate(row.id)" />
                <el-button circle type="danger" icon="el-icon-delete" size="mini" @click="del(row.id)" />
              </el-row>
            </template>
          </el-table-column>

        </el-table>
      </div>
      <!-- ! 分页区域 -->
      <div class="page">
        <el-row type="flex" justify="end">
          <el-pagination
            background
            :current-page="query.page"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="query.limit"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </div>
      <!-- 弹层区域 -->
      <EditAndAdd ref="myDialog" @updateList="initDate" />
    </div>
  </div>
</template>
<script>
// ! 导出
import { exportData } from '@/utils/export'
import Nav from '@/components/comment/index.vue'
import EditAndAdd from '@/components/add&edit/cate.vue'
import { getArticleCate, deleteArticleCate } from '@/api/article/cate'
export default {
  components: {
    Nav,
    EditAndAdd
  },
  data() {
    return {
      // ! 导出数据
      aoa: [],
      list: [],
      query: {
        catename: '',
        page: 1,
        limit: 10
      },
      total: 0

    }
  },

  created() {
    this.initDate()
  },
  methods: {

    // ! 删除操作
    del(id) {
      this.$confirm('确定删除吗,是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // ! 删除请求
        await deleteArticleCate(id)
        this.initDate()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // ! 分页
    handleSizeChange(value) {
      this.query.limit = value
      this.query.page = 1
      this.initDate()
    },
    handleCurrentChange(value) {
      this.query.page = value
      this.initDate()
    },
    // ! 筛选
    search() {
      this.query.page = 1
      this.initDate()
    },
    // ! 重置
    reset() {
      this.query = {
        catename: '',
        page: 1,
        limit: 10
      }
      this.initDate()
    },
    // ! 导出
    exportData() {
      this.list.forEach((item, index) => {
        this.aoa.push([index + 1, item.id, item.catename, item.icon, item.sort_num])
      })
      exportData(this.list, ['#', '分类ID', '分类名称', '图标class', '权重'], this.aoa)
    },
    // ! 渲染数据
    async initDate() {
      const res = await getArticleCate(this.query)
      this.list = res.data
      this.total = res.count
    }
  }
}
</script>
<style lang="scss" scoped>
  .page-box {
    margin: 20px;
    padding: 20px;
    background-color: #fff;
    .search-box,.tabbar-box {
      padding-top: 10px;
      height: 50px;
    }
  }
     ::v-deep .el-dialog__header{
   padding: 15px 20px 10px!important;
    border-bottom: 1px solid #e9ecef;
    background-color: #fafafa;
  }

</style>
